package com.wechatapp.mengyao.poetry.core;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.region.Region;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URL;
import java.util.Date;
import java.util.Map;

/**
 * @author yanghuaxu
 * @date 2025/7/5 15:04
 */
@Component
public class TencentCosUtil {

    @Value("${tencent.cos.region}")
    private String region;
    @Value("${tencent.cos.bucket}")
    private String bucket;
    // 预签名的有效时间，默认为一个小时
    @Value("${tencent.cos.presign.url.expireTime}")
    private Integer presignUrlExpireTime;

    @Autowired
    private COSClient cosClient;

    public PreSigned generatePreSignedUrl(String filePath, Map<String, String> params, Map<String, String> headers) {
        Date expirationDate = new Date(System.currentTimeMillis() + presignUrlExpireTime * 60 * 1000);
        HttpMethodName method = HttpMethodName.GET;
        URL url = cosClient.generatePresignedUrl(bucket, filePath, expirationDate, method, headers, params);
        return new PreSigned(url.toString(), expirationDate);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class PreSigned {
        private String url;

        private Date expirationDate;
    }

}
